package com.kikidao.app.dao;

import com.kikidao.app.exceptions.DAOUncheckedException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/20/12
 * Time: 3:50 PM
 */


public class DAOProperty {
    private static final String propertyFileName = "conn.properties";
    private static final Properties properties = new Properties();
    private String DBName;

    static {

        InputStream propertiesFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyFileName);
        if (propertiesFile == null) {
            throw new DAOUncheckedException("Can't find property file " + propertyFileName + " .");
        }
        try {
            properties.load(propertiesFile);
        } catch (IOException e) {
            throw new DAOUncheckedException("Can't load property file " + propertyFileName + " .", e);
        }

    }

    public DAOProperty(String DBName) {
        this.DBName = DBName;
    }

    public String getProperty(String propertyName, boolean isVital) throws DAOUncheckedException {

        String property = properties.getProperty(DBName + "." + propertyName);
        if (property == null && isVital)
            throw new DAOUncheckedException("Vital property missing from " + propertyFileName + " .");
        return property;
    }
}
